# coding=utf-8
"""
    备份后通知钉钉

    已实现 :
        开始备份时, 通知钉钉
        备份完毕时, 通知钉钉
    待实现 :
        服务异常时, 通知 ( 监听到服务退出, 通知钉钉 )
        备份失败时, 通知 ( 添加备份时的异常捕获, 有异常时通知 )
"""

import os

import requests

from main.do_utils import sql_log
from main.do_utils import local_ip

# @Time    :  2024-06-06 09:47:50
# @Author  :  jiangtong
# @Email   :  gingerqgyy@outlook.com
# @Project :  mysql_backup
# @File    :  ding_talk

os.environ['no_proxy'] = 'teambition.com, dingtalk.com, oapi.dingtalk.com'

# { apk通知 } 群 { teambition apk download }
_url = 'https://oapi.dingtalk.com/robot/send?access_token=4dff8b2b2eb5f3b491f3e9b61870959bd9e586ede570a8705c4c933a4b55283b'


# { api测试 } 群 { api_test }
# _url = 'https://oapi.dingtalk.com/robot/send?access_token=cc0fca1dd383656423ab5863ac768dfedddf8e30abb6e630bc7a3695b3a068d5'


# 开始备份时进行通知 :
def ding_send_begin(db_conf, url=None):
    """ 仅起到通知作用 """
    _ip = local_ip
    _text = f'数据库 < {db_conf.get("host")}:{db_conf.get("port")} > \n开始执行定时备份任务 \n'
    _data = {
        'at': {
            # 'atMobiles': ['17600597837', '13161745902', '18092214620', ],
            'atMobiles': [],
            'isAtAll': False
        },
        'text': {'content': _text},
        'msgtype': 'text'
    }
    _res = requests.post(url=url if url else _url, json=_data)
    sql_log.debug(f'钉钉通知的响应: << {_res.json()} >>')


# 单个数据库备份完成时进行通知 :
def ding_send_one(db, backup_file, url=None):
    """ 仅起到通知作用 """
    _ip = local_ip
    _text = f'数据库 < {db} > 备份成功\n文件名 < {backup_file} > \n位置在 http://{_ip}:8000/SQL/backups/{backup_file}'
    _data = {
        'at': {
            # 'atMobiles': ['17600597837', '13161745902', '18092214620', ],
            'atMobiles': [],
            'isAtAll': False
        },
        'text': {'content': _text},
        'msgtype': 'text'
    }
    _res = requests.post(url=url if url else _url, json=_data)
    sql_log.debug(f'钉钉通知的响应: << {_res.json()} >>')


# 全部数据库备份完成时进行通知 :
def ding_send_all(consume_time=None, url=None, customer_folder=None):
    _ip = local_ip
    _title = f'备份完毕, 耗时 < {consume_time if consume_time else "未统计耗时"} >'
    _t = f'点击链接, 直接前往查看备份列表'
    _pic_url = 'https://yl-prescription-share.oss-cn-beijing.aliyuncs.com/beta/ENT_DISEASE_USER/20240226/8fad6ea6017b4e21ae1cadd1bb158345.png'
    backup_folder = customer_folder if customer_folder else BACKUP_FOLDER
    folder_name = os.path.basename(backup_folder)
    _msg_url = f'http://{_ip}:8000/SQL/{folder_name}'
    _data = {
        'msgtype': 'link',
        'link': {
            'text': _t,
            'title': _title, 'picUrl': _pic_url,
            'messageUrl': _msg_url
        }
    }
    _res = requests.post(url=url if url else _url, json=_data)

    if _res and _res.status_code == 200:
        sql_log.debug(f'通知钉钉成功, 响应结果: {_res.json()}')
    else:
        sql_log.debug(f'通知钉钉失败.')


# 恢复脚本创建完成进行通知 :
def ding_send_recover(db_conf, url=None):
    """ 仅起到通知作用 """
    _ip = local_ip
    _text = f'数据库 < {db_conf.get("host")}:{db_conf.get("port")} > \n备份完成, 恢复命令文件创建成功 \n'
    _data = {
        'at': {
            # 'atMobiles': ['17600597837', '13161745902', '18092214620', ],
            'atMobiles': [],
            'isAtAll': False
        },
        'text': {'content': _text},
        'msgtype': 'text'
    }
    _res = requests.post(url=url if url else _url, json=_data)
    sql_log.debug(f'钉钉通知的响应: << {_res.json()} >>')


if __name__ == '__main__':
    from main import DB_INFO_OWN, BACKUP_FOLDER

    # api_test : { api测试 } 群通知 :
    _url = 'https://oapi.dingtalk.com/robot/send?access_token=cc0fca1dd383656423ab5863ac768dfedddf8e30abb6e630bc7a3695b3a068d5'
    ding_send_begin(DB_INFO_OWN, url=_url)
    # ding_send_one('jsf-ssm', 'jsf-ssm_20240606_114635.sql', url=_url)
    # ding_send_all(url=_url)
    ding_send_all(url=_url, consume_time='10分钟', customer_folder=r'D:/Useless/Share/SQL/custom_backup')
